package com.hy.study.queue;

import java.util.Scanner;

/**
 * 环形的队列
 */
public class CircleArrayQueue {
    public static void main(String[] args) {
        CircleArray circleArrayQueue=new CircleArray(4);//这里设置的4 其队列的有效数据是3
        char key = ' ';//接受用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出队列");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列中取数据");
            System.out.println("h(head):查看队列头的数据");
            key = scanner.next().charAt(0);//接受一个字符
            switch (key) {
                case 's':
                    circleArrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    circleArrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int result = circleArrayQueue.getQueue();
                        System.out.printf("取出的数据是:%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = circleArrayQueue.headQueue();
                        System.out.printf("队列头的数据是:%d\n", head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }


}

class CircleArray {
    private int maxSize;//表示数组的最大容量
    private int front;//指向队列的第一个元素
    private int rear;//指向队列的最后一个元素的后一个位置
    private int[] arr;//该数组用于存放数据，模拟队列

    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = 0;
        rear = 0;
    }

    /**
     * 判断是否满了
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }


    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满,不能加入数据~");
            return;
        }
        //直接将数据添加到数组
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 获取数据
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，无法再取数据");
        }
        //这里需要分析出front 是指向队列的第一个元素
        //1.先吧front对应的值保留到一个临时变量
        int value = arr[front];
        //2.将front后移考量取模
        front = (front + 1) % maxSize;
        return value;
    }

    /**
     * 显示队列的所有数据
     */
    public void showQueue() {

        if (isEmpty()) {
            System.out.println("队列为空，没有数据～");
            return;

        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", (i % maxSize), arr[i % maxSize]);

        }

    }

    /**
     * 获取当前队列的有效个数
     *
     * @return
     */

    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    /**
     * 打印对头的元素
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,没有数据~~");
        }
        return arr[front];
    }
}